package com.ggboy.sc.pl.project.es.util;

import org.springframework.stereotype.Component;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Component("httpUtils")
public class HttpUtils {
    public static String sendGet(String url, String param) {
        StringBuffer result = new StringBuffer();
        BufferedReader in = null;
        InputStream is = null;
        InputStreamReader isr = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            URLConnection connection = realUrl.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.connect();
            Map<String, List<String>> map = connection.getHeaderFields();
            is = connection.getInputStream();
            isr = new InputStreamReader(is, "UTF-8");
            in = new BufferedReader(isr);
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            clese(in, is, isr);
        }
        return result.toString();
    }

    private static void clese(BufferedReader in, InputStream is, InputStreamReader isr) {
        try {
            if (is != null) {
                is.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

    public static String sendPost(String urlString, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        StringBuffer result = new StringBuffer();
        try {
            URL realUrl = new URL(urlString);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(param);
            out.flush();
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != out) {
                    out.close();
                }
                if (null != in) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result.toString();
    }


    public static void main(String[] args) {
        try {
//            URLEncoder.encode(str, "UTF-8");
//
//            URLDecoder.decode(str, "UTF-8");
            //http://localhost:9200/_analyze?analyzer=ik_smart&text=%E5%90%88%E8%82%A5%E5%B8%82%E6%B1%BD%E8%BD%A6%E7%BE%8E%E5%AE%B9%E8%A1%8C%E4%B8%9A%E6%96%87%E6%9C%AC%E6%B5%81%E9%BB%84%E9%98%9F
            String params = URLEncoder.encode("合肥市汽车美容行业文本流黄队", "UTF-8");
            params = URLEncoder.encode("尊敬的省委书记、省长、省人大常委会主任各位高级首,长，您们好‘,我们是安庆、池州、铜陵、芜湖、马鞍山、宣城等地区,的全体革命伤残军人。我们现在联名报告给您们省委书记和,各位高级首长目的，是因为北京、上海、浙江、山东、福建,等数省市地方省政府对我们为国家受伤的残废军人，除国家,补贴的伤残金以外，如上各省市政府按照国家补贴伤残金标,准又给予1OO％的补贴，只有福建省按照国家伤残金标准外给,予6O诉卜贴。,尊敬的省委书记，省委各位高级首长，我们安徽在全国,中部地区，经济发展一直处于领先，省电台也曾多次播出。,外省民众一直都说“福建不如安徽”，原因就是福建都是山,区，和我们黄山地区一样，没有发展空间。福建只有厦门、,泉州、漳州、沿海等几个地区经济发达，其他地区都是深山,老林，经济落后。在此，他们都能给予为国家受伤的残废军,人按照国家标准补贴外又给予补贴6O％。例如，国家补贴给,每位伤残军人1年／1万元，而福建又给补贴6OOO元，难道,说我们安徽省在您们省委各位高级首长的领导下，如此美好,的今天，都不能为我们这些为了国家而受了重轻伤的革命伤,残军人，还有人民警察而表示您们政府的心意吗".replace(",", "").replace("、", "").replace("，", "").replace("‘", ""), "UTF-8");
            String str;
            str = sendGet("http://localhost:9200/_analyze", "analyzer=ik_smart&text=".concat(params));
            System.out.println(str);
            str = sendPost("http://localhost:9200/_analyze?analyzer=ik_smart&text=".concat(params), "");
            //String str= get("https://localhost:443/ssl/test.shtml?name=12&page=34","GBK");
            /*Map<String,String> map = new HashMap<String,String>();
            map.put("name", "111");
            map.put("page", "222");
            String str= postForm("https://localhost:443/ssl/test.shtml",map,null, 10000, 10000);*/
            System.out.println(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
    
    
